package com.example.onlinemusicserver2024.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.onlinemusicserver2024.common.R;
import com.example.onlinemusicserver2024.mapper.SongListMapper;
import com.example.onlinemusicserver2024.model.domain.SongList;
import com.example.onlinemusicserver2024.service.SongListService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SongListServiceImpl extends ServiceImpl<SongListMapper, SongList> implements SongListService {
    @Autowired
    SongListMapper songListMapper;

    @Override
    public R songList(String style) {
        QueryWrapper<SongList> queryWrapper;
        if (style == null || style == "" || "undefined".equals(style)) {
            queryWrapper = null;
        } else {
            queryWrapper = new QueryWrapper<>();
            queryWrapper.like("style", style);
        }
        List<SongList> list = songListMapper.selectList(queryWrapper);
        return R.success("查询成功", list);
    }

    @Override
    public R songUpdate(SongList songList) {
        // 更新歌单信息
        // 先校验歌单是否存在
        SongList objSongList = songListMapper.selectById(songList.getId());
        if (objSongList != null) {
            // 数据存在，可以更新
            BeanUtils.copyProperties(songList, objSongList);
            int count = songListMapper.updateById(objSongList);
            if (count > 0) {
                return R.success("更新成功");
            } else {
                return R.error("更新失败");
            }
        } else {
            // 数据不存在
            return R.error("数据不存在，请确认");
        }
    }

    @Override
    public R songListByStyle(String style) {
        QueryWrapper<SongList> queryWrapper;
        if (style == null || "全部歌单".equals(style) || style == "") {
            queryWrapper = null;
        } else {
            queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("style", style);
        }
        List<SongList> songLists = songListMapper.selectList(queryWrapper);
        return R.success("查询成功", songLists);
    }

    @Override
    public R songListByTitle(String title) {
        QueryWrapper<SongList> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title",title);
        List<SongList> songLists = songListMapper.selectList(queryWrapper);
        return R.success("查询成功",songLists);
    }
}
